//
//  NetWorkHandler.m
//  HomeworkJSON_9.16
//
//  Created by amtlib on 14-9-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NetWorkHandler.h"

@implementation NetWorkHandler


+ (void)getConnectionWithURL:(NSString *)str completion:(void(^)(id result))block
{
    
    NSString *newStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:newStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    
    request.HTTPMethod = @"get";
    
    // 2、连接服务器
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (data != nil) {
            
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            // 解析之后的结果 通过block参数 使用
            block(result);
            
            
        }
        
        
    }];


}
+ (void)getPhotoConnectionWithURL:(NSString *)str completion:(void(^)(id result))block
{
    
    NSString *newStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:newStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    
    request.HTTPMethod = @"get";
    [request setValue:@"IMSM-3" forHTTPHeaderField:@"IC-App-V"];
    [request setValue:@"IMSM-0.2.1" forHTTPHeaderField:@"IC-App"];
    
    // 2、连接服务器
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (data != nil) {
            
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            // 解析之后的结果 通过block参数 使用
            block(result);
            
            
        }
        
        
    }];
    
    
}




@end
